package com.example.yang_android_seventh;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import android.os.Bundle;

import static android.hardware.SensorManager.*;





//备注，本app没有在虚拟机进行调试，使用真机作为调试，所以虚拟机可能报错







public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager mSensorManager;
    EditText etTxt1;
    EditText etTxt2;
    EditText etTxt3;
    EditText etTxt4;
    EditText etTxt5;
    EditText etTxt6;
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etTxt1 =findViewById(R.id.txt1);
        etTxt2 =findViewById(R.id.txt2);
        etTxt3 =findViewById(R.id.txt3);
        etTxt4 =findViewById(R.id.txt4);
        etTxt5 =findViewById(R.id.txt5);
        etTxt6 =findViewById(R.id.txt6);
        button1=findViewById(R.id.entre);
        // 获取传感器模拟器的传感器管理服务
        mSensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);
        // 连接传感器模拟器
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,uc1.class);
                startActivity(intent);
            }
        });

    }
    @Override
    protected void onResume()
    {
        super.onResume();
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
                SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SENSOR_DELAY_GAME);

    };
    @Override
    protected void onStop()
    {
        mSensorManager.unregisterListener(this);
        super.onStop();
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values=event.values;
        int sensorType=event.sensor.getType();
        StringBuilder sb=null;
        switch(sensorType) {
            case Sensor.TYPE_ACCELEROMETER:
                sb=new StringBuilder();
                sb.append("X方向上的加速度：");
                sb.append(values[0]);
                sb.append("\nY方向上的加速度：");
                sb.append(values[1]);
                sb.append("\nZ方向上的加速度：");
                sb.append(values[2]);
                etTxt1.setText(sb.toString());
                break;
            case Sensor.TYPE_ORIENTATION:
                sb=new StringBuilder();
                sb.append("绕X转过的角度：");
                sb.append(values[0]);
                sb.append("\n绕Y转过的角度：");
                sb.append(values[1]);
                sb.append("\n绕Z转过的角度：");
                sb.append(values[2]);
                etTxt2.setText(sb.toString());
                break;
            case Sensor.TYPE_GRAVITY:
                sb=new StringBuilder();
                sb.append("X轴方向的重力：");
                sb.append(values[0]);
                sb.append("\nY轴方向的重力：");
                sb.append(values[1]);
                sb.append("\nZ轴方向的重力：");
                sb.append(values[2]);
                etTxt3.setText(sb.toString());
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                sb=new StringBuilder();
                sb.append("X轴方向的磁场强度：");
                sb.append(values[0]);
                sb.append("\nY轴方向的磁场强度：");
                sb.append(values[1]);
                sb.append("\nZ轴方向的磁场强度：");
                sb.append(values[2]);
                etTxt4.setText(sb.toString());
                break;
            case Sensor.TYPE_GYROSCOPE:
                sb=new StringBuilder();
                sb.append("绕X转过的角速度：");
                sb.append(values[0]);
                sb.append("\n绕Y转过的角速度：");
                sb.append(values[1]);
                sb.append("\n绕Z转过的角速度：");
                sb.append(values[2]);
                etTxt5.setText(sb.toString());
                break;
            case Sensor.TYPE_LIGHT:
                sb=new StringBuilder();
                sb.append("当前光照强度：");
                sb.append(values[0]);
                etTxt6.setText(sb.toString());
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
